package com.egg.auth.controller.feign;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egg.auth.entity.user.User;
import com.egg.auth.feign.AuthUserFeignClient;
import com.egg.auth.model.LoginUserInfo;
import com.egg.auth.model.request.web.UserPagePostFeign;
import com.egg.auth.model.response.web.UserPage;
import com.egg.auth.service.login.LoginHelper;
import com.egg.auth.service.user.IUserService;
import com.egg.common.core.model.controller.FeignController;
import com.egg.common.core.model.response.PageData;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.OptionalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;


@Slf4j
@Api(tags = FeignController.API_PREFIX + "用户中心")
@RestController
@RequestMapping("/user")
public class FeignUserController extends FeignController implements AuthUserFeignClient {


    @Autowired
    private IUserService userService;


    @Override
    @ApiOperation(value = "feign用户分页查询")
    @PostMapping("/page")
    public ResponseData<PageData<UserPage>> userPagePost(@RequestBody UserPagePostFeign param) {
        if (ObjectUtil.isNotEmpty(param.getIds())) {
            param.setCurrent(1);
            param.setPageSize(param.getIds().size());
        }
        Integer current = ObjectUtil.isNotEmpty(param.getCurrent()) ? param.getCurrent() : 1;
        Integer pageSize = ObjectUtil.isNotEmpty(param.getPageSize()) ? param.getPageSize() : 1000;
        Page<User> dbPage = userService.page(
                current, pageSize
                , OptionalUtil.build(param.getIds())
                , OptionalUtil.build(param.getRealName())
                , OptionalUtil.build(param.getNickname())
                , OptionalUtil.build(param.getPhone())
                , OptionalUtil.build(param.getState())
                , OptionalUtil.build(param.getGender())
                , OptionalUtil.build(param.getOnline())
                , OptionalUtil.build(param.getRealNameOrPhone())
                , Optional.empty()
        );
        return ResponseData.success(new PageData<>(dbPage, userService.convertUserPage(dbPage.getRecords())));
    }

    @Override
    @ApiOperation(value = "通过TOKEN查询用户信息")
    @GetMapping("/login/info/token")
    public ResponseData<LoginUserInfo> userLoginInfoByToken(
            @ApiParam(value = "令牌") @RequestParam(value = "token", required = false) String token
    ) {
        if (ObjectUtil.isEmpty(token)) {
            return ResponseData.error();
        }
        Object loginId = StpUtil.getLoginIdByToken(token);
        if (null == loginId) {
            return ResponseData.error("已下线或失效，请尝试重新登录");
        }
        LoginUserInfo loginUserInfo = LoginHelper.getLoginUser(token);
        return ResponseData.success(loginUserInfo);
    }


}
